<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>商品详情</title>
  <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
  <meta content="yes" name="apple-mobile-web-app-capable"/>
  <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
  <meta content="telephone=no" name="format-detection"/>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="__STATIC__/layer/layer.js"></script>
</head>
<body>
商品名称：<p>{$goods.name}</p>
商品简介：<p>{$goods.description}</p>
产品口味：巧克力  蜂蜜  丝滑  醇香<br/>
商品价格：￥<span id="price">{$goods.goods_price}</span><br/>
库   存：{$goods.stock}<br/>

<!--商品轮播图区  start-->
{if condition="!empty($goods.images)"}
  {foreach name="goods.images" item="v"}
    <img src="{$v}" width="100px;" height="100px;">
  {/foreach}
{/if}
<!--商品轮播图区  end-->
<form action="{:url('GoodsList/pre_create_order')}" method="post" onsubmit="return checkform();">
<!--商品属性区  start-->
{volist name="spec_name" id="v" key="kk"}
    <div>
        <p style="border:1px solid red;" id="spec_{$kk}">{$v.name}</p>
        {foreach name="spec_result" item="v1"}
              {if condition="$v1.spec_id eq $v.id"}
                  <input type="radio" name="spec_{$kk}"  value="{$v1.id}" onclick="get_price();">{$v1.detail}
              {/if}
        {/foreach}
    </div>
{/volist}
<!--商品属性区  end-->
购买数量：<input type="text" name="num" value="1">
<input type="hidden" name="spec_count" value="{$spec_count}">
<input type="hidden" name="goods_id" value="{$goods.goods_id}">
<input type="text" name="id[]" value="">

<div>
  <input type="submit" value="立即购买" >&nbsp;&nbsp;<a href="javascript:;" onclick="addto_cart()">加入购物车</a>&nbsp;&nbsp;
        {if condition="$is_collect eq 1"}
        <span id="collect">
            <a href="javascript:;" onclick="collect_goods(0)">取消收藏</a>
        </span>
        {else /}
        <span id="collect">
            <a href="javascript:;" onclick="collect_goods(1)">收藏</a>
        </span>
        {/if}
</div>
</form>
<!--商品详情 start-->
{$goods.description}
<!--商品详情 end-->



</body>
</html>
<script>
  /**
   *  获取价格
   */
  function get_price(){
      var spec_num = "{$spec_count}";
      var goods_id = $('input[name=goods_id]').val();
      //商品属性组合
      var spec_combin = '';
      for(var i=1;i<=spec_num;i++){
          if($('input[name=spec_'+i+']:checked').val() == null){
              return false;
          }
          if($('input[name=spec_'+i+']:checked').val() != null){
              spec_combin += $('input[name=spec_'+i+']:checked').val()+',';
          }
      }
      $.post("{:url('GoodsList/pre_calculate_price')}",
          {goods_id:goods_id,specs:spec_combin},
          function(data){
              if(data.code == 1){
                  $('input[name="id[]"]').val(data.data.id);
                  $('#price').text(data.data.price);
              }else{
                  $('input:radio[name^="spec_"]').removeAttr('checked');
                  alert(data.msg);
              }
          }
      );
  }

  /**
   *  添加到购物车
   */
  function addto_cart(){
      //商品属性数量
      var id = $('input[name="id[]"]').val();
      var spec_num = "{$spec_count}";
      var spec_combin = '';
      //商品id
      var goods_id = $('input[name=goods_id]').val();
      for(var i=1;i<=spec_num;i++){
          if($('input[name=spec_'+i+']:checked').val() == null){
              var spec_name = $('#spec_'+i).text();
              alert('请选择'+spec_name);
              return false;
          }
          if($('input[name=spec_'+i+']:checked').val() != null){
              spec_combin += $('input[name=spec_'+i+']:checked').val()+',';
          }
      }
      $.post("{:url('GoodsList/addto_cart')}",
          {id:id,goods_id:goods_id,specs:spec_combin},
          function(data){
              if(data.code == 1){
                  $('input[name=id]').val(data.data);
                  alert('添加购物车成功');
              }else{
                  alert(data.msg);
              }
          }
      );
  }

  /**
   *  立即购买
   */
  function checkform(){
      //store_goods_stock  =>  主键id
//      var id = $('input[name="id[]"]').val();
      //商品属性数量
      var spec_num = "{$spec_count}";
      var spec_combin = '';
      //商品id
      var goods_id = $('input[name=goods_id]').val();
      for(var i=1;i<=spec_num;i++){
          if($('input[name=spec_'+i+']:checked').val() == null){
              var spec_name = $('#spec_'+i).text();
              alert('请选择'+spec_name);
              return false;
          }
          if($('input[name=spec_'+i+']:checked').val() != null){
              spec_combin += $('input[name=spec_'+i+']:checked').val()+',';
          }
      }
      return true;
  }

  /**
   * 收藏商品
   */
  function collect_goods(type){
      var goods_id = $("input[name=goods_id]").val();
      var store_id = "{$goods.store_id}";
      $.post("{:url('GoodsList/collect_goods')}",
          {goods_id:goods_id,store_id:store_id,type:type},
          function(data){
                if(data.code == 1){
                    if(type == 1){
                        alert('收藏成功');
                        var ht = '<a href="javascript:;" onclick="collect_goods(0)">取消收藏</a>';
                        $('#collect').html(ht);
                    }else{
                        alert('取消成功');
                        var ht = '<a href="javascript:;" onclick="collect_goods(1)">收藏</a>';
                        $('#collect').html(ht);
                    }
                }else{
                    if(type == 1){
                        alert('收藏失败');
                    }else{
                        alert('取消失败');
                    }
                }
          }
      );
  }
</script>